package com.demo2do.fms.admin.web.controller.supplier.reform;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.search.supplier.SupplierSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.supplier.SupplierService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.entity.supplier.common.AuditStatus;
import com.demo2do.fms.entity.supplier.common.Supplier;
import com.demo2do.fms.entity.supplier.common.Type;

/**
 * 分属运输供方绩效管理
 *
 * @author Ark
 *
 */
@Controller
@RequestMapping("/appraisal-rectify-suppliers")
@MenuSetting("menu-supplier")
public class AppraisalRectifySupplierController {
    
    public static final Logger logger = LoggerFactory.getLogger(AppraisalRectifySupplierController.class);
    
    @Autowired
    private SupplierService supplierService;

    /**
     * 列出所有合格或整改状态的供方
     * 
     * @param login
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-trans-supplier-appraisal-management")
    public ModelAndView list(@Secure Login login, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/appraisal/appraisal-rectify-supplier/appraisal-rectify-supplier-list");
        
        int totals = supplierService.countRectifySupplier(login);
        if (totals > 0) {
            
            List<Supplier> suppliers = supplierService.listRectifySupplier(login, page.with(totals));
            
            modelAndView.addObject("suppliers", suppliers);
            modelAndView.addObject("page", page);
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 过滤查询 - 过滤所有合格或整改状态的供方 - modal
     * 
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView search() {
        ModelAndView modelAndView = new ModelAndView("supplier/appraisal/appraisal-rectify-supplier/appraisal-rectify-supplier-search");
        return modelAndView;
    }
    
    /**
     * 过滤查询 - 过滤所有合格或整改状态的供方
     * 
     * @param search
     * @param login
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-trans-supplier-appraisal-management")
    public ModelAndView onSearch(SupplierSearch search, @Secure Login login,
                                 HttpServletRequest request, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/appraisal/appraisal-rectify-supplier/appraisal-rectify-supplier-list");
        
        search.setType(Type.MOTOR);
        if (login.isSupplier()) {
        	List<Long> supplierIds = supplierService.listSubSupplierId(login);
    		search.addSupplierIds(supplierIds);
        }
        
        int totals = supplierService.countRectify(search);
        if (totals > 0) {
            
            List<Supplier> suppliers = supplierService.listRectify(search, page.with(totals));
            
            modelAndView.addObject("suppliers", suppliers);
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 整改 - modal
     * 
     * @param supplierId
     * @return
     */
    @RequestMapping(value = "/{supplierId}/rectify", method = RequestMethod.GET)
    public ModelAndView rectify(@PathVariable("supplierId") Long supplierId) {

        ModelAndView modelAndView = new ModelAndView("supplier/appraisal/appraisal-rectify-supplier/appraisal-rectify-supplier-rectify");

        Supplier supplier = supplierService.load(Supplier.class, supplierId);
        modelAndView.addObject("supplier", supplier);

        return modelAndView;

    }

    /**
     * @param operator
     * @param supplierId
     * @param status
     * @return
     */
    @RequestMapping(value = "/{supplierId}/rectify", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onRectify(@Secure(property = "id") Long operator, @PathVariable("supplierId") Long supplierId, @RequestParam("status") AuditStatus status) {

        HttpResult httpResult = new HttpResult();

        try {

            Supplier supplier = supplierService.load(Supplier.class, supplierId);
            supplierService.rectify(supplier, status, operator);

            httpResult.success().message("整改成功");

        } catch (Exception e) {
            httpResult.fail("500").message("整改失败");
            logger.error("#onRectify() - rectify fail, exception = [{}]", e);
        }

        return httpResult;

    }
    
}
